﻿using System;
using System.Collections.Generic;
using System.IO;
using USU.MetricsMonitor.Common;
using USU.MetricsMonitor.Metrics;

namespace USU.MetricsMonitor.Storage
{
    public class StorageProvider
    {
        private const string PROJECTS_STORAGE = "projects";

        private readonly string _directory;
        private readonly IStorage _storage;

        public string StorageDirectory
        {
            get { return _directory; }
        }
        
        public StorageProvider( string directory )
        {
            _directory = directory;
            _storage = new XmlStorage();
        }
        public StorageProvider( IStorage storage, string directory )
        {
            _storage = storage;
            _directory = directory;
        }

        public string PathToProjectDirectory( string projectName )
        {
            return Path.Combine(_directory, projectName);
        }

        public void CreateProjectDirectory(string projectName)
        {
            string path = PathToProjectDirectory(projectName);
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
        }

        private string PathToMetric( string projectName, string metricTypeName )
        {
            return Path.Combine(
                PathToProjectDirectory( projectName ),
                String.Format( "{0}.{1}",metricTypeName,_storage.DefaultFileExtension )
            );
        }

        public void SaveMetrics( string projectName, IEnumerable<MetricCollection> metrics )
        {
            throw new NotImplementedException();
        }

        public void SaveMetric<T>( string projectName, T metric )
        {
            _storage.Save( PathToMetric( projectName, metric.GetType().Name ), metric );
        }

        public T LoadMetric<T>( string projectName )
        {
            return _storage.Load<T>( PathToMetric( projectName, typeof(T).Name ) );    
        }

        public IEnumerable<MetricCollection> LoadMetrics( string projectName )
        {
            throw new NotImplementedException();
        }

        private string PathToProjects()
        {
            return Path.Combine(
                _directory,
                String.Format( "{0}.{1}", PROJECTS_STORAGE, _storage.DefaultFileExtension)
            );
        }
        
        public void AddProject( ProjectInfo project )
        {
            var projects = LoadProjects();

            projects.RemoveAll(p => { return p.Name == project.Name; });
            projects.Add( project );

            SaveProjects( projects );
        }

        public void RemoveProject( ProjectInfo project )
        {
            var projects = LoadProjects();
            if ( projects.Remove( project ) ) _storage.Save( PathToProjects(), projects );
        }

        public List<ProjectInfo> LoadProjects()
        {
           return _storage.Exists( PathToProjects() )
               ? _storage.Load<List<ProjectInfo>>( PathToProjects() ) : new List<ProjectInfo>();
        }

        public void SaveProjects(List<ProjectInfo> projects)
        {
            _storage.Save<List<ProjectInfo>>(PathToProjects(), projects);
        }
    }
}